package com.lanswon.qzsmk.controller.systemManageController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.controller.cardManageController.BaseController;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.model.Role;
import com.lanswon.qzsmk.model.User;
import com.lanswon.qzsmk.service.systemManageService.BranchManageService;
import com.lanswon.qzsmk.service.systemManageService.ParamManageService;
import com.lanswon.qzsmk.service.systemManageService.TellerManageService;
import com.lanswon.qzsmk.vo.SaveOrUpdateTellerVo;
import com.lanswon.qzsmk.vo.TellerAuthVo;
import com.lanswon.qzsmk.vo.TellerVo;


/**
 * 柜员管理Controller
 * @author guyawei
 *
 */
@Controller
@RequestMapping(value="/tellerManage")
public class TellerManageController  {

	@Autowired
	TellerManageService tms;

	@Autowired
	ParamManageService pm;
	
	@Autowired
	BranchManageService bms;
	/**
	 * 链接柜员管理页面
	 * @return
	 */
	@RequiresPermissions("tellerManage:index")
	@RequestMapping(value="/tellerManageIndex",name="柜员管理,tellerManage:index",method={RequestMethod.GET,RequestMethod.POST})
	public String tellerManage(){
		
		return "tellerManage";
	}
	
	/**
	 * 链接增加柜员页面
	 * @return
	 */
	@RequiresPermissions("tellerManage:addTeller")
	@RequestMapping(value="/addTeller",name="柜员管理增加柜员页面,tellerManage:addTeller",method=RequestMethod.GET)
	public String addTeller(HttpServletRequest request){
		request.getSession().setAttribute("sign", true);
		return "AddTeller";
	}
		
	/**
	 * 链接修改页面
	 * @param parm
	 * @return
	 * @throws Exception 
	 */
	@RequiresPermissions("tellerManage:goUpdateTeller")
	@RequestMapping(value="/goUpdateTeller",name="柜员管理修改页面,tellerManage:goUpdateTeller",method=RequestMethod.GET)
	public String goUpdateTeller(String parm,HttpServletRequest request) throws Exception{
		request.getSession().setAttribute("sign", false);
		SaveOrUpdateTellerVo teller=tms.queryUserByUserNo(Integer.valueOf(parm));
		request.getSession().setAttribute("teller", teller);
		return "AddTeller";
	}
	
	/**
	 * 给柜员分配网点
	 * @param parm
	 * @param request
	 * @return
	 * @throws NumberFormatException
	 * @throws Exception
	 */
	@RequiresPermissions("tellerManage:allotBranch")
	@RequestMapping(value="/allotBranch",name="柜员管理给柜员分配网点,tellerManage:allotBranch",method=RequestMethod.GET)
	public String allotBranch(String parm,HttpServletRequest request) throws NumberFormatException, Exception{
		request.getSession().setAttribute("sign", false);//修改标识
		request.getSession().setAttribute("allot", true);//分配标识
		SaveOrUpdateTellerVo teller=tms.queryUserByUserNo(Integer.valueOf(parm));
		request.getSession().setAttribute("teller", teller);
		return "AddTeller";
	}
	
	/**
	 * 获取柜员信息
	 * @param tv
	 * @param pageCount
	 * @return
	 */
	@RequiresPermissions("tellerManage:getTellerInfo")
	@RequestMapping(value="/getTellerInfo",name="柜员管理获取柜员信息,tellerManage:getTellerInfo",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<User> getTellerInfo(TellerVo tv,Integer pageNum,Integer numPerPage){
//		if(tv.getNumPerPage()==null){//加载页面时执行
//			tv.setCurrentPage(1);
//			tv.setNumPerPage(20);
//		}else {
//			if(tv.getCurrentPage()<=1||pageCount==0){
//				tv.setCurrentPage(1);
//			}else if(tv.getCurrentPage()>=pageCount&&pageCount!=0){
//				tv.setCurrentPage(pageCount);
//			}
//		}
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;			
		}
		tv.setCurrentPage(pageNum);
		tv.setNumPerPage(numPerPage);
		tv.setStartLine((tv.getCurrentPage()-1)*tv.getNumPerPage());
		DwzPage<User> dPage=tms.queryTellerByName(tv);
		return dPage;
	}	
	
	/**
	 * 获取要修改的信息
	 * @param request
	 * @return
	 */
	@RequiresPermissions("tellerManage:getUpdateTellerInfo")
	@ResponseBody
	@RequestMapping(value="/getUpdateTellerInfo",name="柜员管理获取要修改的信息,tellerManage:getUpdateTellerInfo",method=RequestMethod.POST)
	public Map<String, Object> getUpdateInfo(HttpServletRequest request){
		Map<String, Object> map=new HashMap<>();
		map.put("sign",request.getSession().getAttribute("sign"));
		map.put("teller",request.getSession().getAttribute("teller"));
		map.put("allot",request.getSession().getAttribute("allot"));
		request.getSession().removeAttribute("sign");
		request.getSession().removeAttribute("teller");
		request.getSession().removeAttribute("allot");
		return map;	
	}
	
	/**
	 * 修改或新增柜员
	 * @param teller
	 * @return
	 * @throws ParseException 
	 * @throws ApplicationException 
	 */
	@RequiresPermissions("tellerManage:updateTeller")
	@RequestMapping(value="/updateTeller",name="柜员管理提交修改或新增柜员,tellerManage:updateTeller",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateTeller(SaveOrUpdateTellerVo tellerVo) throws ParseException, ApplicationException{
		Map<String, Object> map=new HashMap<>();
		if(tellerVo.getUserNo()==null){//新增
			System.out.println("新增了");
			tms.addTeller(tellerVo);
		}else{//修改
			System.out.println("修改了");
			tms.modifyTeller(tellerVo);
		}
		map.put("statusCode", "200");
		map.put("message", "操作成功");
		map.put("navTabId", "tellerManage");
		map.put("callbackType", "closeCurrent");
		map.put("forwardUrl", "tellerManage/tellerManageIndex");//post请求
		return map;
	}
		
	/**
	 * 链接柜员授权页面
	 * @return
	 */
	@RequiresPermissions("tellerAuth:index")
	@RequestMapping(value="/tellerAccreditIndex",name="柜员授权,tellerAuth:index",method=RequestMethod.GET)
	public String tellerAccreditIndex(){
		return "TellerAccredit";
	}
	
	/**
	 * 柜员授权获取柜员信息
	 * @param tv
	 * @param pageCount
	 * @return
	 */
	@RequiresPermissions("tellerAuth:getInfoByTellerAccredit")
	@ResponseBody
	@RequestMapping(value="/getInfoByTellerAccredit",name="柜员授权获取柜员信息,tellerAuth:getInfoByTellerAccredit",method=RequestMethod.POST)
	public DwzPage<TellerAuthVo> getInfoByTellerAccredit(TellerAuthVo tv,Integer pageNum,Integer numPerPage){
//		if(tv.getNumPerPage()==null){//加载页面时执行
//			tv.setCurrentPage(1);
//			tv.setNumPerPage(20);
//		}else {
//			if(tv.getCurrentPage()<=1||pageCount==0){
//				tv.setCurrentPage(1);
//			}else if(tv.getCurrentPage()>=pageCount&&pageCount!=0){
//				tv.setCurrentPage(pageCount);
//			}
//		}
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;			
		}
		tv.setCurrentPage(pageNum);
		tv.setNumPerPage(numPerPage);
		tv.setStartLine((tv.getCurrentPage()-1)*tv.getNumPerPage());
		DwzPage<TellerAuthVo> dPage=tms.queryTellerByUserNameOrUserNo(tv);
		return dPage;
		
	}
	
	/**
	 * 柜员授权====》角色设置
	 * @param request
	 * @param parm
	 * @return
	 * @throws Exception 
	 * @throws NumberFormatException 
	 */
	@RequiresPermissions("tellerAuth:roleSet")
	@RequestMapping(value="/roleSet",name="柜员授权角色设置,tellerAuth:roleSet",method=RequestMethod.GET)
	public String roleSet(String userId,HttpServletRequest request) throws NumberFormatException, Exception{
		System.out.println(userId);
		String[] array=userId.split(",");
		SaveOrUpdateTellerVo user=tms.queryUserByUserNo(Integer.valueOf(array[1]));
		Map<String, List<Role>> map1=tms.queryRoleByUser(Integer.valueOf(array[0]), 1);//默认系统ID为1
		Map<String, Object> map=new HashMap<>();
		map.put("map", map1);
		map.put("user", user);
		request.getSession().setAttribute("map", map);
		return "RoleSet";
	}
	
	/**
	 * 获取柜员有哪些角色
	 * @param userId
	 */
	@RequiresPermissions("tellerAuth:getRoleByTeller")
	@ResponseBody
	@RequestMapping(value="/getRoleByTeller",name="柜员授权获取柜员有哪些角色,tellerAuth:getRoleByTeller",method=RequestMethod.POST)
	public Map<String, Object> getRoleByTeller(HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String, Object> map=(Map<String, Object>) request.getSession().getAttribute("map");
		request.getSession().removeAttribute("map");
		return map;
	}
	
}
